package com.chatTools.message;

import com.chatToolsRpc.message.RpcRequestMessage;
import com.chatToolsRpc.message.RpcResponseMessage;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： pang-yun
 * @date： 2021-09-16 11:12
 * 功能目的 ：用于  抽象消息父类
 */

@Data
@ToString
public abstract class Message implements Serializable {


    public static Class<?> getMessageClass(int messageType) {
        return messageClasses.get(messageType);
    }

    // 请求序号
    private int sequenceId;

    private int messageType;

    // 抽象方法   子类实现  知道 自己属于那个对象  type 是 ？
    public abstract int getMessageType();

    private static final Map<Integer, Class<?>> messageClasses = new HashMap<>();

    //-----------------消息----------------------
    public static final int LoginRequestMessage = 0;
    public static final int LoginResponseMessage = 1;
    public static final int ChatRequestMessage = 2;
    public static final int ChatResponseMessage = 3;
    public static final int GroupCreateRequestMessage = 4;
    public static final int GroupCreateResponseMessage = 5;
    public static final int GroupJoinRequestMessage = 6;
    public static final int GroupJoinResponseMessage = 7;
    public static final int GroupQuitRequestMessage = 8;
    public static final int GroupQuitResponseMessage = 9;
    public static final int GroupChatRequestMessage = 10;
    public static final int GroupChatResponseMessage = 11;
    public static final int GroupMemberRequestMessage = 12;
    public static final int GroupMemberResponseMessage = 13;

    //------心跳消息-----ping pong message----
    public static final int PingMessage = 14;
    public static final int PongMessage = 15;

    //-----rpc 消息重构---
    public static final int RPC_MESSAGE_TYPE_REQUEST = 101;
    public static final int RPC_MESSAGE_TYPE_RESPONSE = 102;


    static {
        messageClasses.put(LoginRequestMessage, LoginRequestMessage.class);
        messageClasses.put(LoginResponseMessage, LoginResponseMessage.class);
        messageClasses.put(ChatRequestMessage, ChatRequestMessage.class);
        messageClasses.put(ChatResponseMessage, ChatResponseMessage.class);
        messageClasses.put(GroupCreateRequestMessage, GroupCreateRequestMessage.class);
        messageClasses.put(GroupCreateResponseMessage, GroupCreateResponseMessage.class);
        messageClasses.put(GroupJoinRequestMessage, GroupJoinRequestMessage.class);
        messageClasses.put(GroupJoinResponseMessage, GroupJoinResponseMessage.class);
        messageClasses.put(GroupQuitRequestMessage, GroupQuitRequestMessage.class);
        messageClasses.put(GroupQuitResponseMessage, GroupQuitResponseMessage.class);
        messageClasses.put(GroupChatRequestMessage, GroupChatRequestMessage.class);
        messageClasses.put(GroupChatResponseMessage, GroupChatResponseMessage.class);
        messageClasses.put(GroupMemberRequestMessage, GroupMemberRequestMessage.class);
        messageClasses.put(GroupMemberResponseMessage, GroupMemberResponseMessage.class);

        messageClasses.put(RPC_MESSAGE_TYPE_RESPONSE,RpcRequestMessage.class);

        //--------rpc -----
        messageClasses.put(RPC_MESSAGE_TYPE_REQUEST, RpcRequestMessage.class);
        messageClasses.put(RPC_MESSAGE_TYPE_RESPONSE, RpcResponseMessage.class);
    }
}
